package com.example.demo.controller;

import com.example.demo.entry.Person;
import com.example.demo.entry.PersonAttributesMapper;
import com.example.demo.entry.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 认证
 *
 * @author: YZD
 * @Date: 2020-04-29 18:05
 */
@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private LdapTemplate ldapTemplate;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/byCn")
    public boolean authByCn(@RequestParam(required = false) String userName, @RequestParam(required = false) String passWord) {
        // "Manager", "yzd1206"
        EqualsFilter filter = new EqualsFilter("cn", userName);
        boolean authenticate = ldapTemplate.authenticate("", filter.toString(), passWord);
        logger.info(authenticate + "");
        return authenticate;
    }

    @GetMapping("/byUid")
    public boolean authByUid(@RequestParam(required = false) String uid, @RequestParam(required = false) String passWord) {
        // "yzd", "yzd1206"
        EqualsFilter filter = new EqualsFilter("uid", uid);
        boolean authenticate = ldapTemplate.authenticate("", filter.toString(), passWord);
        logger.info(authenticate + "");
        return authenticate;
    }

    @GetMapping("/all")
    public Result all(@RequestParam(required = false) String name, @RequestParam(required = false) String pwd) {
        Result result = new Result();
        try {
            EqualsFilter filterByUid = new EqualsFilter("uid", name);
            boolean authenticateByUid = ldapTemplate.authenticate("", filterByUid.toString(), pwd);
            EqualsFilter filterByCn = new EqualsFilter("cn", name);
            boolean authenticateByCn = ldapTemplate.authenticate("", filterByCn.toString(), pwd);
            if (authenticateByUid || authenticateByCn) {
                logger.info(authenticateByUid + "-" + authenticateByCn);
                AndFilter filter = new AndFilter();
                filter.and(new EqualsFilter("objectClass", "Person"));
                List<Person> users = ldapTemplate.search("", filter.encode(), new PersonAttributesMapper());
                for (Person user : users) {
                    if (user.getCn().equals(name) || user.getUid().equals(name)) {
                        result.setName(user.getCn());
                        if (!StringUtils.isEmpty(user.getOu())) {
                            result.setGruop(user.getOu());
                        }
                        if (!StringUtils.isEmpty(user.getO())) {
                            result.setOrganization(user.getO());
                        }
                    }
                    return result;
                }
            }
        } catch (Exception e) {
            logger.error("error :", e);
        }
        return null;
    }
}
